import {Map, List} from 'immutable';

import * as Constant from '../constants/HgConstant';
import {RES_OK_CODE, RES_MAINTAIN_CODE} from '../../../constants/AppConstant'; 

const initState = Map({
  sportBalls: {}, // 球类列表
  ballTypes: [], // 球类盘口
  ballPeiyu: {}, // 球类盘口下的赔率
  apiRes: {},
  sportDetail: {}, // 体育详情
  orderItems: [],
  isMaintain: false,
  maintainMsg: '',
});

export default function Huangguan(state = initState, action) {
  
  switch (action.type) {
    case Constant.REQUEST_SPORT_BALL:
      let sports = action.data.datas.datas;
      let sportBalls = {};
      for (let sport of sports) {
        sportBalls[sport.rType] = sport.rName;
      }
      state = state.set('sportBalls', sportBalls);
      state = state.set('ballTypes', []);
      state = state.set('ballPeiyu', {});
      state = state.set('sportDetail', {});
      state = state.set('apiRes', action.data);
      if (RES_MAINTAIN_CODE == action.data.errorCode) {
        state = state.set('isMaintain', true);
        state = state.set('maintainMsg', action.data.msg);
      } else {
        state = state.set('isMaintain', false);
        state = state.set('maintainMsg', '');
      }
      break;
    case Constant.REQUEST_SPORT_BALL_TYPES:
      let ball = action.data.ball;
      state = state.set('ballTypes', action.data.datas.datas);
      state = state.set('apiRes', action.data);
      if (RES_MAINTAIN_CODE == action.data.errorCode) {
        state = state.set('isMaintain', true);
        state = state.set('maintainMsg', action.data.msg);
      } else {
        state = state.set('isMaintain', false);
        state = state.set('maintainMsg', '');
      }
      break;
    case Constant.REQUEST_SPORT_PEIYU:
      state = state.set('apiRes', action.data);
      if (RES_MAINTAIN_CODE == action.data.errorCode) {
        state = state.set('isMaintain', true);
        state = state.set('maintainMsg', action.data.msg);
      } else {
        state = state.set('ballPeiyu', action.data.datas);
        state = state.set('isMaintain', false);
        state = state.set('maintainMsg', '');
      }
      break;
    case Constant.REQUEST_SPORT_DETAIL:
      if (RES_MAINTAIN_CODE == action.data.errorCode) {
        state = state.set('isMaintain', true);
        state = state.set('maintainMsg', action.data.msg);
      } else {
        state = state.set('sportDetail', action.data.datas);
        state = state.set('apiRes', action.data);
        state = state.set('isMaintain', false);
        state = state.set('maintainMsg', '');
      }
      break;

    case Constant.REQUEST_ORDER_ITEMS:

      let pageNo = action.data.pageNo;
      if (action.data.datas) {
        if (pageNo == 1) {
          state = state.set('orderItems', action.data.datas.list);  
        } else {
          let list = state.get('orderItems');
          if (typeof action.data.datas.list != 'undefined') {
            list = list.concat(action.data.datas.list);  
          }
          
          state = state.set('orderItems', list);
        }
      }
      
      state = state.set('apiRes', action.data);
      break;
  }

  return state;
}

